package org.web.base;

import com.dbb.trade.service.PhotoManager;
import com.dbb.trade.util.TreeNode;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，

 * 真正登录的POST请求由Filter完成,
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/file")
public class FileController {
    @Autowired
    private PhotoManager photoManager;

	@RequestMapping()
	public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
                       @RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request) {

        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        model.addAttribute("pages", photoManager.findAll(searchParams, pageNumber, 15, sortType));
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        model.addAttribute("sortType", sortType);
		return "file/list";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model) {
		model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
		return "account/login";
	}

    @RequestMapping("/tree")
    public @ResponseBody String getTree(ServletRequest request) {
        System.out.println(request.getParameter("dir"));
        TreeNode rootNode = new TreeNode("root", null, null, null);
        TreeNode n1 = new TreeNode("D:\\images\\apache-solr-4.0.0\\dist");
        TreeNode n2 = new TreeNode("D:\\photo\\100NCD80");
        TreeNode n3 = new TreeNode("C:\\swsetup\\SP49415\\WinWDF");
        List<TreeNode> ths = new ArrayList<TreeNode>();
        ths.add(n1);
        ths.add(n2);
        ths.add(n3);
        rootNode.addChildren(ths);
//        if("/".equals(dir)) {
            return rootNode.toTree();
//        }
//        System.out.println(dir);
//        System.out.println(rootNode);
//        System.out.println(rootNode.containId(dir));
//        return rootNode.containId(dir).toTree();

    }

}
